package com.xiaotu.spider.weibo.pageprocessor;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVWeiboModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;

/**
 * @类名 WeiboPageProcessor
 * @日期 2017年2月15日
 * @作者 高海军
 * @功能 作品新浪微博数据爬取
 */
@Component("TVWeiboPageProcessor")
@Scope("prototype")
public class TVWeiboPageProcessor extends APageProcessor
{
    protected static final String SEARCH_URL = "http://huati.weibo.com/k/";

    @Override
    protected Request getStartRequest(JSONObject target)
    {
        return new Request(SEARCH_URL + KeywordEncode.encode(target.getString(this
                .getKeyWordFieldName())));
    }

    protected String getKeyWordFieldName()
    {
        return "tvName";
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.TV_WEIBO;
    }

    @Override
    public String getPageDataType(Page page)
    {
        if (page.getHtml() == null || page.getHtml().toString().contains("parent.window.location"))
            return Constants.PageDataType.INVALID_DATA;
        return Constants.PageDataType.WEIBO;
    }

    @Override
    public void process(Page page)
    {
        Map<String, Object> tvMap = page.getRequest().getExtras();
        if (Constants.PageDataType.INVALID_DATA.equals(getPageDataType(page)))
            this.forwardProcess(page, tvMap);
        else
            this.dataProcess(page, tvMap);
    }

    private void forwardProcess(Page page, Map<String, Object> tvMap)
    {
        String url = RegexUtils.regexSearch("parent.window.location=\"/[a-z]/.+/super_index\"",
				page.getHtml().toString());
        if (StringUtils.isEmpty(url))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        url = RegexUtils.replaceStr(url, "parent.window.location=|\"", StringUtils.EMPTY);
        if (StringUtils.isEmpty(url))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        PageUtils.addTargetRequest(page, "http://weibo.com/" + url, tvMap);
    }

    protected void dataProcess(Page page, Map<String, Object> tvMap)
    {
        String[] data = this.getWeiboData(page);
        page.putField(TVWeiboModel.class.getSimpleName(), new TVWeiboModel(tvMap.get("tvid") +
				"", tvMap.get("tvName") + "", data[2], data[0], data[1], null, null, "2"));
    }

    protected String[] getWeiboData(Page page)
    {
        List<String> labelList = RegexUtils.regexSearchAll(
                "<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong><span class=\\\\\"(.*?)" +
						"\\\\\">阅读<\\\\/span>|"
                        + "<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong><span class=\\\\\"(" +
						".*?)\\\\\">讨论<\\\\/span>|"
                        + "<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong><span class=\\\\\"(" +
						".*?)\\\\\">帖子<\\\\/span>|"
                        + "<strong class=\\\\\"(.*?)\\\\\">(.*)<\\\\/strong><span class=\\\\\"(" +
						".*?)\\\\\">粉丝<\\\\/span>",
                page.getRawText());

        if (labelList == null || labelList.isEmpty())
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);

        String read = null, comment = null, fans = null;
        boolean noData = true;
        for (String label : labelList)
        {
            List<String> valueList = RegexUtils.regexSearchAll("<strong class=\\\\\"(.*?)\\\\\">(" +
					".*)<\\\\/strong>|<span class=\\\\\"(.*?)\\\\\">(.*)<\\\\/span>", label);
            if (valueList == null || valueList.size() < 2)
                continue;
            String value = HtmlUtil.delHTMLTag(valueList.get(0));
            String tag = HtmlUtil.delHTMLTag(valueList.get(1));
            switch (tag)
            {
                case "阅读":
                    read = value;
                    noData = false;
                    break;
                case "讨论":
                case "帖子":
                    comment = value;
                    noData = false;
                    break;
                case "粉丝":
                    fans = value;
                    noData = false;
                    break;
            }
        }

        if (noData)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);

        return new String[]{read, comment, fans};
    }
}
